package com.wy.read_write_lock_demo;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author 美女
 * @date 2022/05/07 13:26
 **/
public class Main {
    public static void main(String[] args) {
        ReadWriteLock readWriteLock=new ReentrantReadWriteLock();
        Lock readLock = readWriteLock.readLock();
        Lock writeLock=readWriteLock.writeLock();
        //“写”的角色，请求写锁
        //“读”的角色，请求读锁
        writeLock.lock();//写锁已经有了
        Thread t=new Thread(){
            @Override
            public void run() {
                readLock.lock();
                System.out.println("子线程也可以加读锁成功");//读和读不做互斥
            }
        };
        t.start();
    }
}
